package com.store.bookstore_backend.service.impl;

import com.store.bookstore_backend.domain.Address;
import com.store.bookstore_backend.domain.User;
import com.store.bookstore_backend.dto.AddressAddDTO;
import com.store.bookstore_backend.dto.AddressGetDTO;
import com.store.bookstore_backend.dto.AddressUpdateDTO;
import com.store.bookstore_backend.exception.BookStoreException;
import com.store.bookstore_backend.mapper.AddressMapper;
import com.store.bookstore_backend.mapper.UserMapper;
import com.store.bookstore_backend.service.AddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public List<AddressGetDTO> getAddress(HttpServletRequest request) {
        // 获取用户ID
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            throw new IllegalArgumentException("User ID is missing in the request.");
        }

        // 获取地址列表和用户信息
        List<Address> addressList = addressMapper.selectAllAddress(userId);
        User user = userMapper.getUserById(userId);
        System.out.println(">>>>>>>  "+user.toString());
        // 构建返回结果
        List<AddressGetDTO> addressGetDTOList = new ArrayList<>();
        for (Address address : addressList) {
            AddressGetDTO addressGetDTO = new AddressGetDTO();
            // 复制字段
            BeanUtils.copyProperties(address, addressGetDTO);
            // 设置默认值
            addressGetDTO.setDefaultValue(address.getId().equals(user.getDefaultAddressId()));
            addressGetDTOList.add(addressGetDTO);
        }

        return addressGetDTOList;
    }


    @Override
    public void createAddress(AddressAddDTO addressAddDTO) {
        Address address = new Address();
        address.setPhone(addressAddDTO.getPhone());
        address.setDetailAddr(addressAddDTO.getDetailAddr());
        address.setUserId(addressAddDTO.getUserId());
        address.setReceiverName(addressAddDTO.getReceiverName());
        address.setUrbanAddr(addressAddDTO.getUrbanAddr());
        addressMapper.insertAddress(address);
    }

    @Override
    public void deleteAddress(int id) {
        addressMapper.deleteAddress(id);
    }

    @Override
    public void updateAddress(AddressUpdateDTO addressUpdateDTO) {
        Address address = new Address();
        address.setPhone(addressUpdateDTO.getPhone());
        address.setDetailAddr(addressUpdateDTO.getDetailAddr());
        address.setUrbanAddr(addressUpdateDTO.getUrbanAddr());
        address.setId(addressUpdateDTO.getId());
        address.setReceiverName(addressUpdateDTO.getReceiverName());
        addressMapper.updateAddress(address);
    }

    @Override
    public void setDefaultAddress(int addressId, HttpServletRequest request
    ) {
        Long userId = (Long) request.getAttribute("userId");
        userMapper.setDefaultAddress(addressId, userId);
    }

    @Override
    public void cancelDefault(int addressId, HttpServletRequest request
    ) {
        try {
            // 从JWT中解析用户ID
            Long userId = (Long) request.getAttribute("userId");

            userMapper.setDefaultAddress(null, userId);
        } catch (Exception e) {
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_TOKEN_IS_INVALID);
        }
    }
}
